package com.brkj.test;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.httpInterface.HttpInterface.HIF_GetExamDetail;
import com.brkj.main.R;
import com.brkj.test.model.DS_Exam;
import com.brkj.test.model.DS_Exam_detail_ques;
import com.brkj.test.model.DS_QuesRecord;
import com.brkj.util.ConstAnts;
import com.dgl.sdk.util.DBStore;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalDb;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class RealExamAdapter extends BaseAdapter{

	private ListItemView listItemView;
	protected LayoutInflater listContainer;
	private Context context;
	private List<DS_Exam> examList;
	private boolean isOffLineList;

	public RealExamAdapter(Context context, List<DS_Exam> examList, boolean isOffLineList){
		this.context = context;
		this.examList = examList;
		this.isOffLineList = isOffLineList;

		listContainer = LayoutInflater.from(context);

	}
	
	private static class ListItemView{
		public TextView Title;
		public TextView Time;
		public TextView quesNum;
		public TextView studyTimes;
		public Button downloadExam;
	};
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return examList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 自定义视图
		listItemView = new ListItemView();
		// 获取list_item布局文件的视图
		if (convertView == null) {
			convertView = listContainer.inflate(R.layout.exam_list_item, null);
			// 获取控件对象
			listItemView.Title = (TextView) convertView
					.findViewById(R.id.Title);
			listItemView.Time = (TextView) convertView
					.findViewById(R.id.Time);
			listItemView.quesNum = (TextView) convertView
					.findViewById(R.id.quesNum);
			listItemView.studyTimes = (TextView) convertView
					.findViewById(R.id.studyTimes);
			listItemView.downloadExam = (Button) convertView
					.findViewById(R.id.downloadExam);

			// 设置控件集到convertView
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}
		
		
		listItemView.Title.setText(examList.get(position).getExamPaperName());
		listItemView.Time.setText(examList.get(position).getTime());
		listItemView.quesNum.setText("试题量："+examList.get(position).getTotalNum());

        listItemView.studyTimes.setVisibility(View.GONE);
//		listItemView.studyTimes.setText("已做题数：" +
//				readHaveDoneQues(Integer.toString(examList.get(position).getExamPaperID())));

        if(isOffLineList){
            listItemView.downloadExam.setVisibility(View.GONE);
        }else{

            if(examList.get(position).isDownload()){
                listItemView.downloadExam.setText("已下载");
                listItemView.downloadExam.setBackgroundResource(android.R.color.transparent);
            }else{

                listItemView.downloadExam.setText("下载试题");
                listItemView.downloadExam.setBackgroundResource(R.drawable.btn_1_selector);

                listItemView.downloadExam.setTag(examList.get(position));
                listItemView.downloadExam.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        downlaodExam((DS_Exam)v.getTag(), v);
                    }
                });
            }

        }


		convertView.setOnClickListener(new clickListener(position));
		
		return convertView;
	}

	protected void downlaodExam(final DS_Exam ques, final View v) {

        ques.setDownload(true);

		DBStore.save(ques, ConstAnts.DB_TABLE_REALL_EXAM_OFFLINE_QUES_LIST);
		
		BaseAjaxParams params = new BaseAjaxParams();
		params.put(HttpInterface.HIF_GetRealExamDetail.params.ExamPaperID, Integer.toString(ques.getExamPaperID()));
		
		final ProgressDialog progressDialog = new ProgressDialog(context);
		progressDialog.setMessage("下载试题中...");
		progressDialog.show();
		
		FinalHttp fh = new FinalHttp();
		fh.post(HttpInterface.HIF_GetRealExamDetail.address, params, new AjaxCallBack<Object>() {

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				List<DS_Exam_detail_ques> quesList =  new ArrayList<DS_Exam_detail_ques>();
				
				Gson gson = new Gson();
				Type listType = new TypeToken<List<DS_Exam_detail_ques>>(){}.getType();
				quesList = gson.fromJson((String)t, listType);
				
				if(quesList.size()>0){
                    final List<DS_Exam_detail_ques> finalQuesList = quesList;
                    new Thread(){
                        @Override
                        public void run() {
                            DBStore.saveList(finalQuesList,
                                    ConstAnts.DB_TABLE_REALL_EXAM_OFFLINE_QUES_DETAIL + ques.getExamPaperID());
                        }
                    }.start();

				}
				
				progressDialog.dismiss();
				
				//下载失效
				((TextView)v).setText("已下载");
                v.setBackgroundResource(android.R.color.transparent);
				v.setOnClickListener(null);
			}
			
		});
	}

	private class clickListener implements OnClickListener{

		private int pos;
		
		public clickListener(int position){
			this.pos = position;
		}
		
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(context, RealExamDetailActivity.class);
			intent.putExtra("exam", (Serializable)examList.get(pos));
			((Activity)context).startActivityForResult(intent, 1);
		}
		
	}

}
